﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using LinkCard.AdminCP.Common;
using System.Data;
namespace LinkCard.AdminCP.Message
{
    public partial class message_edit : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }
            if (!IsPostBack)
            {
                Int64 nMessageID  = ConvertUtility.ToInt64(Request.QueryString["MessageID"]);
                int nCategoryID = 0;
                if (nMessageID > 0) {
                    MessageID.Value = nMessageID.ToString();

                    string sSQL = "Select * from message where MessageID="+nMessageID;
                    DataTable dtMess = DataHelper.SelectTable(sSQL, false);
                    if (dtMess.Rows.Count > 0) {

                        MsgHistory.Value =HttpUtility.HtmlEncode( dtMess.Rows[0]["MsgHistory"].ToString());
                        MsgTitle.Value = dtMess.Rows[0]["MsgTitle"].ToString();
                        MsgSubTitle.Value = dtMess.Rows[0]["MsgSubTitle"].ToString();

                        MsgPostDate.Value = dtMess.Rows[0]["MsgPostDate"].ToString();

                        MsgContent.Value = dtMess.Rows[0]["MsgContent"].ToString();

                        MsgLead.Value = dtMess.Rows[0]["MsgLead"].ToString();
                        MsgHomeImage.Value = dtMess.Rows[0]["MsgHomeImage"].ToString();
                        MsgImageComment.Value = dtMess.Rows[0]["MsgImageComment"].ToString();
                        nCategoryID = ConvertUtility.ToInt32(dtMess.Rows[0]["CategoryID"]);
                        txtTags.Value = dtMess.Rows[0]["Tags"].ToString();

                    }
                }
                
                DataTable dt;
                // Category Option 
                dt = DataHelper.SelectTable("SELECT CategoryID, Name FROM MessageCate ORDER BY Name", false);
                CategoryID.DataSource = dt;
                CategoryID.DataTextField = "Name";
                CategoryID.DataValueField = "CategoryID";
                CategoryID.DataBind();
                CategoryID.SelectedValue = nCategoryID.ToString();

                MsgPostDate.Value = DateTime.Now.ToString();
                MsgHomeImage.ToolbarSet = "Image";
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            update();
            Response.Redirect("/Message/message_all_published.aspx");
        }

        protected void update()
        {
            var dao = new LinkCard.Dao.MessageDao();
            var item = new LinkCard.Model.Message();
            string sMsgHomeImage = MsgHomeImage.Value.Replace("<p>", "");
            sMsgHomeImage = sMsgHomeImage.Replace("</p>", "");
            sMsgHomeImage = sMsgHomeImage.Replace("<P>", "");
            sMsgHomeImage = sMsgHomeImage.Replace("</P>", "");

            item.MessageID = ConvertUtility.ToInt64(MessageID.Value);

            item.MsgTitle = MsgTitle.Value;
            item.MsgSubTitle = MsgSubTitle.Value;
            item.MsgLead = MsgLead.Value;
            item.MsgContent = MsgContent.Value;
            item.MsgHomeImage = sMsgHomeImage;
            item.MsgImageComment = MsgImageComment.Value;
            item.MsgPostDate = ConvertUtility.ToDateTime(MsgPostDate.Value);
            item.CategoryID = ConvertUtility.ToInt32(CategoryID.SelectedValue);
            item.MsgHistory = MsgHistory.Value +  "<br>Bài viết được sửa online bởi " + Session["USER"].ToString() + " lúc " + DateTime.Now.ToString() + "";
            item.Tags = txtTags.Value;
            dao.Update(item);
            if (!string.IsNullOrEmpty(item.Tags))
            {
                var addTag = new TagDao().AddMessageTag(item.MessageID, item.Tags);
            }
        }

        protected Int64 getUserID()
        {

            DataTable dt = DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }
    }
}
